//
//  ZYCategorySecondCollectionReusableView.m
//  Adorable_Things
//
//  Created by huzhiyan on 16/1/12.
//  Copyright © 2016年 Banana Inc. All rights reserved.
//

#import "ZYCategorySecondCollectionReusableView.h"
#import "ZYCategoryMainCollectionViewCell.h"
#import "ZYCategoryMainModel.h"

@interface ZYCategorySecondCollectionReusableView () <UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) NSMutableArray *dataModelArray;
@property (nonatomic, strong) UICollectionView *collectionView;


@end

@implementation ZYCategorySecondCollectionReusableView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self addSubCollectionViewItemSize];
        [self setUpData];
    }
    return self;
}

- (void)setParent_id:(NSString *)parent_id
{
    _parent_id = parent_id;
    [self setUpData];
    [_collectionView reloadData];
}

- (void)setUpData
{
    NSMutableDictionary *parentDic = [NSMutableDictionary dictionary];
    parentDic[@"parent_id"] = _parent_id;
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager POST:@"http://api.menghuoapp.com/v1/category/list" parameters:parentDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic  = (NSDictionary *)responseObject;
        NSArray *dataArr = dic[@"data"];
        _dataModelArray = [NSMutableArray array];
        for (NSDictionary *dataDic in dataArr) {
            ZYCategoryMainModel *model = [[ZYCategoryMainModel alloc] init];
            [model setValuesForKeysWithDictionary:dataDic];
            [_dataModelArray addObject:model];
        }
        [_collectionView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];

}

- (void)addSubCollectionViewItemSize
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 20, 10, 20);
    flowLayout.itemSize = CGSizeMake((kWidth * 80) / 375, (kWidth * 80) / 375);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kWidth / 3.75) collectionViewLayout:flowLayout];
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor clearColor];
    [self addSubview:_collectionView];
    [self.collectionView registerNib:[UINib nibWithNibName:@"ZYCategoryMainCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"ZYMainCollectionViewCell"];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataModelArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZYCategoryMainCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZYMainCollectionViewCell" forIndexPath:indexPath];
    ZYCategoryMainModel *model = _dataModelArray[indexPath.row];
    cell.model = model;
    cell.layer.cornerRadius = 5;
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((kWidth * 80) / 375, (kWidth * 80) / 375);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZYCategoryMainModel *model = _dataModelArray[indexPath.row];
//    _block(model.category_id);
    if ([_delegate respondsToSelector:@selector(ZYCategoryCollectionReusableView:parentID:)]) {
        [_delegate ZYCategoryCollectionReusableView:self parentID:model.category_id];
    }
}

- (void)awakeFromNib {
    // Initialization code
}

@end
